<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="../assets/logo.jpg" alt="" />
        <span>橙实电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button></el-header
    >
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="target_tool" @click="handleMenu">
          <i class="el-icon-s-tools"></i>
        </div>
        <el-menu
          background-color="#282a36"
          text-color="#fff"
          active-text-color="#1495E7"
          unique-opened
          :collapse-transition="false"
          router
          :collapse="isCollapse"
          :default-active="activePath"
          >
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item @click="chooseMenu(item2.path)" :index="item2.path" v-for="item2 in item.children" :key="item2.id">
              <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{ item2.authName }}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList:[],
      iconObj:{
        '125': 'iconfont el-icon-user',
        '103' :'iconfont el-icon-setting',
        '101':'iconfont el-icon-house',
        '102':'iconfont el-icon-tickets',
        '145':'iconfont el-icon-receiving'
      },
      //是否折叠
      isCollapse:false,
      //被激活的链接地址
      activePath:'',
    };
  },
  created(){
    this.getMenuList()
    this.activePath = sessionStorage.getItem('activePath')
  },
  mounted() {},

  methods: {
    logout() {
      sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取菜单
    async getMenuList(){
       const {data:res} = await this.$http.get('menus')
      if (res.meta.status !==200) {
        return this.$message.error(res.meta.msg)
      }
      this.menuList = res.data
    },
    handleMenu(){
      this.isCollapse = !this.isCollapse
    },
    chooseMenu(activePath){
      sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }
  },
};
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #424451;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  img {
    width: 60px;
    height: 60px;
    border-radius: 50px;
  }
  span {
    margin-left: 10px;
  }
}
.el-aside {
  background-color: #282a36;
  .el-menu {
    height: calc(100% - 38px);
    border-right: none;
  }
  .iconfont{
    font-size: 20px;
    margin-right: 10px;
  }
  .target_tool{
    font-size: 25px;
    color: #fff;
    text-align: center;
    cursor: pointer;
  }
}
.el-main {
  background-color: #fff;
}
</style>
